package com.zlf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlf.blog.dao.mapper.CommentMapper;
import com.zlf.blog.dao.pojo.CommentVo;
import com.zlf.blog.handle.LoginInterceptor;
import com.zlf.blog.utils.UserThreadLocal;
import com.zlf.blog.vo.*;
import com.zlf.blog.vo.params.PageParams;
import com.zlf.blog.dao.mapper.ArticleMapper;
import com.zlf.blog.dao.pojo.Article;
import com.zlf.blog.service.ArticleService;
import com.zlf.blog.service.SysUserService;
import com.zlf.blog.service.TagsService;
import lombok.RequiredArgsConstructor;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author zlfi1
* @description 针对表【ms_article】的数据库操作Service实现
* @createDate 2022-07-02 09:33:34
*/
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
implements ArticleService{
    private final ArticleMapper articleMapper;
    private final SysUserService sysUserService;
    private final TagsService tagsService;
    private final LoginInterceptor loginInterceptor;
    private final CommentMapper commentMapper;

    public ArticleVo copy(Article article, boolean isAuthor, boolean isBody, boolean isTags){
        ArticleVo articleVo = new ArticleVo();
        BeanUtils.copyProperties(article, articleVo);
        if (isAuthor) {
            String authorId = article.getAuthorId();
            articleVo.setAuthor(sysUserService.findSysUserById(authorId).getNickname());
        }
        articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        if (isTags){
            String articleId=article.getId();
            articleVo.setTags(tagsService.findTagsByArticleId(articleId));
        }
        return articleVo;
    }

    private List<ArticleVo> copyList(List<Article> records,boolean isAuthor,boolean isBody,boolean isTags) {
        List<ArticleVo> articleVoList = new ArrayList<>();
        for (Article article : records) {
            ArticleVo articleVo = copy(article, isAuthor, isBody, isTags);
            articleVoList.add(articleVo);
        }
        return articleVoList;

    }
    @Override
    public Result listArticle(PageParams pageParams) {
        /**
         * 分页查询article数据库表
         *
         */
        Page<Article> page=new Page<>(pageParams.getPage(),pageParams.getPageSize());
        LambdaQueryWrapper<Article> queryWrapper=new LambdaQueryWrapper<>();
        //是否置顶进行排序
        //order by created_date desc
        queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);
        Page<Article> articlePage=articleMapper.selectPage(page,queryWrapper);
        List<Article> records = articlePage.getRecords();
        //不能直接返回
        List<ArticleVo> articleVoList=copyList(records,true,true,true);
        return Result.success(articleVoList);

    }

    @Override
    public List<ArticleVo> userArticle(String authorId) {

        List<ArticleVo> articleVos=articleMapper.selectByAuthorId(authorId);
        List<ArticleVo> articleVoList=new ArrayList<>();
        for (ArticleVo articleVo : articleVos) {
            String id=articleVo.getId();
            List<TagVo> tagVos=articleMapper.selectTag(id);
            articleVo.setTags(tagVos);
            articleVoList.add(articleVo);
        }
        return  articleVoList;
    }

    @Override
    public EditResultVo selectArticleBodyById(String bodyId) {
        EditResultVo editResultVo=articleMapper.selectArticleBodyById(bodyId);
        String articleId= editResultVo.getArticleId();
        List<TagVo> tagVos=articleMapper.selectTag(articleId);
        editResultVo.setTag(tagVos);
        return  editResultVo;
    }

    @Override
    public int updateArticleBodyById(EditArticleVo editArticleVo) {
        String[] tagVos=editArticleVo.getCheckedList();
        String bodyId=editArticleVo.getBodyId();
        ArticleVo articleVo=new ArticleVo();
        articleVo.setId(articleMapper.articleIdByBodyId(bodyId));
        TagVo tagVo=new TagVo();
        for(int i=0;i<tagVos.length;i++){
            tagVo.setId(articleMapper.selectTagId(tagVos[i])); ;
            articleMapper.addArticleTag(articleVo.getId(),tagVo.getId());
        }
        String contentHtml=editArticleVo.getContentHtml();
        String content= contentHtml;
        content = content.replaceAll("<p .*?>", "\r\n");
        // <br><br/>替换为换行
        content = content.replaceAll("<br\\s*/?>", "\r\n");
        // 去掉其它的<>之间的东西
        content = content.replaceAll("\\<.*?>", "");
        editArticleVo.setContent(content);
        editArticleVo.setContentHtml(contentHtml);
        return articleMapper.updateArticleBodyById(editArticleVo);
    }

    @Override
    public int addArticle(ArticleAdd articleAdd) {
        UUID uuid1=UUID.randomUUID();
        UUID uuid2=UUID.randomUUID();
        articleAdd.setArticleId(uuid1.toString().replace("-", ""));
        articleAdd.setBody_Id(uuid2.toString().replace("-", ""));
        articleAdd.setAuthorId(UserThreadLocal.get().getId());
        String content=articleAdd.getContentHtml();
        content = content.replaceAll("<p .*?>", "\r\n");
        // <br><br/>替换为换行
        content = content.replaceAll("<br\\s*/?>", "\r\n");
        // 去掉其它的<>之间的东西
        content = content.replaceAll("\\<.*?>", "");
        articleAdd.setContent(content);
        String[] tagVos=articleAdd.getCheckedList();
        String tagId="";
        for(int i=0;i<tagVos.length;i++){
            tagId=articleMapper.selectTagId(tagVos[i]);
            articleMapper.addArticleTag(articleAdd.getArticleId(), tagId);
        }

        return articleMapper.addArticle(articleAdd);
    }

    @Override
    public int uploadFile(String fileId,String fileName,String userAccount) {
        return articleMapper.uploadFile(fileId,fileName,userAccount);
    }

    @Override
    public int deleteArticle(String articleId) {
        return articleMapper.deleteArticle(articleId);
    }

    @Override
    public ArticleBody selectArticleBodyByArticleId(String articleId) {
        return articleMapper.viewArticle(articleId);
    }

    @Override
    public List<CommentVo> selectComment(String bodyId) {
        List<CommentVo> list= commentMapper.selectComment(bodyId);
        return commentMapper.selectComment(bodyId);
    }

    @Override
    public int addComment(CommentVo commentVo) {
        commentVo.setAuthorId(UserThreadLocal.get().getId());
        commentVo.setToUid(commentMapper.selectUserIdByArticleId(commentVo.getArticleId()));
        commentVo.setId(UUID.randomUUID().toString().replace("-",""));
        return commentMapper.addComment(commentVo);

    }

    @Override
    public int replyComment(CommentVo commentVo) {
        return commentMapper.replyComment(commentVo);

    }


}
